package network;

import model.User;
import model.Product;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.net.Socket;
import java.util.List;
import java.util.Scanner;

public class EStoreClient {
    private String serverAddress = "localhost";
    private int serverPort = 8888;
    private User currentUser = null;
    private Scanner scanner = new Scanner(System.in);

    public void start() {
        while (true) {
            if (currentUser == null) {
                showLoginMenu();
            } else {
                showMainMenu();
            }
        }
    }

    private void showLoginMenu() {
        System.out.println("\n===== 商城登录 =====");
        System.out.println("1. 登录");
        System.out.println("2. 退出");
        System.out.print("请选择：");
        int choice = scanner.nextInt();
        scanner.nextLine(); // 消费换行符

        switch (choice) {
            case 1:
                login();
                break;
            case 2:
                System.exit(0);
            default:
                System.out.println("选择无效，请重试！");
        }
    }

    private void login() {
        System.out.print("请输入用户名：");
        String username = scanner.nextLine();
        System.out.print("请输入密码：");
        String password = scanner.nextLine();

        try (Socket socket = new Socket(serverAddress, serverPort);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

            oos.writeObject("USER_LOGIN");
            oos.writeObject(username);
            oos.writeObject(password);

            currentUser = (User) ois.readObject();
            if (currentUser != null) {
                System.out.println("登录成功！欢迎 " + currentUser.getUsername() + "（" + currentUser.getRole() + "）");
            } else {
                System.out.println("用户名或密码错误！");
            }
        } catch (Exception e) {
            System.out.println("登录失败：" + e.getMessage());
        }
    }

    private void showMainMenu() {
        System.out.println("\n===== 商城主菜单 =====");
        System.out.println("1. 浏览商品");
        if (currentUser.getRole().equals("admin")) {
            System.out.println("2. 查看所有用户（管理员专属）");
        }
        System.out.println("3. 退出登录");
        System.out.print("请选择：");
        int choice = scanner.nextInt();
        scanner.nextLine();

        switch (choice) {
            case 1:
                browseProducts();
                break;
            case 2:
                if (currentUser.getRole().equals("admin")) {
                    getAdminUsers();
                } else {
                    System.out.println("无权限操作！");
                }
                break;
            case 3:
                currentUser = null;
                System.out.println("已退出登录！");
                break;
            default:
                System.out.println("选择无效，请重试！");
        }
    }

    private void browseProducts() {
        try (Socket socket = new Socket(serverAddress, serverPort);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

            oos.writeObject("GET_ALL_PRODUCTS");
            List<Product> products = (List<Product>) ois.readObject();

            System.out.println("\n===== 商品列表 =====");
            for (Product p : products) {
                System.out.println(p);
            }
        } catch (Exception e) {
            System.out.println("浏览商品失败：" + e.getMessage());
        }
    }

    private void getAdminUsers() {
        try (Socket socket = new Socket(serverAddress, serverPort);
             ObjectOutputStream oos = new ObjectOutputStream(socket.getOutputStream());
             ObjectInputStream ois = new ObjectInputStream(socket.getInputStream())) {

            oos.writeObject("ADMIN_GET_ALL_USERS");
            List<User> users = (List<User>) ois.readObject();

            System.out.println("\n===== 所有用户（管理员专属） =====");
            for (User u : users) {
                System.out.println(u.getId() + ". " + u.getUsername() + "（" + u.getRole() + "）");
            }
        } catch (Exception e) {
            System.out.println("获取用户列表失败：" + e.getMessage());
        }
    }

    public static void main(String[] args) {
        new EStoreClient().start();
    }
}